using System;
using Avalonia;
using EmotionChat.Services;
using Microsoft.Extensions.DependencyInjection;
using Mvvm.Models;
using Mvvm.Services;
using MVVM.Services;
using Mvvm.ViewModels;
using Mvvm.Views;

namespace Mvvm;

//服务定位器模式：相当于医院前台，你可以在这里找到service\viewmodel（什么奇怪的比喻）
public class ServiceLocator {
    private static ServiceLocator _current;

    public static ServiceLocator Current
    {
        get
        {
            if (_current is not null)
            {
                return _current;
            }

            if (Application.Current.TryGetResource(nameof(ServiceLocator),
                    null, out var resource) &&
                resource is ServiceLocator serviceLocator)
            {
                return _current = serviceLocator;
            }

            throw new Exception("理论上来讲不应该发生这种情况。");
        }
    }
    private readonly IServiceProvider _serviceProvider;
    //步骤9.3、获取MainWindowViewModel，改名复制到下面即可（写好注释！）
    public MainWindowViewModel MainWindowViewModel =>
        _serviceProvider.GetService<MainWindowViewModel>();
    //复制到这↓

    public FirstPageViewModel FirstPageViewModel =>
        _serviceProvider.GetService<FirstPageViewModel>();
    public PersonalityProfileViewModel PersonalityProfileViewModel => 
        _serviceProvider.GetRequiredService<PersonalityProfileViewModel>();
    // 获取 DepressionTestViewModel
    public DepressionTestViewModel DepressionTestViewModel =>
        _serviceProvider.GetRequiredService<DepressionTestViewModel>();

    public ChatReportViewModel ChatReportViewModel =>
        _serviceProvider.GetService<ChatReportViewModel>();
    public SentimentAnalysisViewModel SentimentAnalysisViewModel =>
        _serviceProvider.GetRequiredService<SentimentAnalysisViewModel>();
    
    public DirectoryViewModel DirectoryViewModel =>
        _serviceProvider.GetRequiredService<DirectoryViewModel>();
    public DepressionAnalysisViewModel DepressionAnalysisViewModel =>
        _serviceProvider.GetService<DepressionAnalysisViewModel>();
    
    //步骤9.1、在构造函数中，注册了所有服务（类）
    public ServiceLocator() {
        //保持不变
        var serviceCollection = new ServiceCollection();
        
        //步骤9.2、注册所有viewmodel，你注册的东西改名复制到下面（写好注释！）
        serviceCollection.AddSingleton<MainWindowViewModel>();//对应步骤6-8创建的类
        serviceCollection.AddSingleton<DepressionAnalysisViewModel>();

        //复制到这↓
        serviceCollection.AddSingleton<FirstPageViewModel>();
        // 注册 PersonalityProfileViewModel
        serviceCollection.AddSingleton<PersonalityProfileViewModel>();
 
        serviceCollection.AddSingleton<DepressionTestViewModel>();
        serviceCollection.AddSingleton<DepressionTestStorage>();
        // 注册XunFeiClient
        serviceCollection.AddSingleton<IXunFeiClient, XunFeiClient>();
        // 注册 DepressionTest 类，它是 `DepressionTestViewModel` 的依赖
        serviceCollection.AddSingleton<DepressionTest>();
        //保持不变
        serviceCollection.AddSingleton<IAlertService, AlertService>();
        serviceCollection.AddSingleton<IEmotionTrajectoryService, EmotionTrajectoryService>();

        //注册SentimentAnalysisViewModel
        serviceCollection.AddSingleton<SentimentAnalysisViewModel>();
        
        // 注册XunFeiClient
        serviceCollection.AddSingleton<XunFeiClient>();
        
        //注册MbtitestViewModel
        serviceCollection.AddSingleton<ChatReportViewModel>();
        // 注册 ChatDataProcessedStorage
        serviceCollection.AddSingleton<IChatDataProcessedStorage, ChatDataProcessedStorage>();
        // 注册 ChatDataStorage
        serviceCollection.AddSingleton<IChatDataStorage, ChatDataStorage>();
        // 注册 ChatDataProcessService
        serviceCollection.AddSingleton< ChatDataProcessService>();
        // 注册EmotionalAnalysisService
        serviceCollection.AddSingleton<EmotionalAnalysisService>();
        serviceCollection.AddSingleton<EmotionReportService>();
        serviceCollection.AddSingleton<IChatTimeReportService, ChatTimeReportService>();
        serviceCollection.AddSingleton<IChatTimeAIAdviceService, ChatTimeAIAdviceService>();
        serviceCollection.AddSingleton<DirectoryViewModel>();

        
        //保持不变
        _serviceProvider = 
            serviceCollection.BuildServiceProvider();
    }
}
//备注：
//引用微软默认的依赖注入容器，这里也安装了这个软件包
//也许这就是“使用依赖注入管理依赖”？
//该部分为实现view与viewmodel的连接创建了一个服务定位器，
//注意，本项目所有的service和viewmodel都是单例（只有一个实例）